#ifndef __READ_FILE_H
#define __READ_FILE_H

#include <glib.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdint.h>
#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define NOTE				(100)
#define NUM				(50)
#define FILE_SIZE			(4096)
#define INPUT_FILE_PATH			"input.txt"

#define TITLE				"title = "
#define PAGE				"page = "

struct all_dentry {
	int pages;
	char title[NUM];
	char note[NOTE];
	char **page_title;
};

struct dentry {
	int dvalue;
	int pegenum;
	char name[NUM];
	char type[NUM];
	char defa[NUM];
	char unit[NUM];
	char note[NOTE];
};

static struct dentry *contents;

static int read_file(const char *filename) 
{
	g_message("enter func %s", __func__);
	int fd = 0;
	gchar *textbuf = NULL;
	if ((fd = g_open(filename, O_RDONLY)) < 0) {
		g_message("%s -- open file error!\n", __func__);
		return -1;
	}
	struct stat buf;
	if (-1 == g_stat(filename, &buf)) {
		g_message("%s -- stat error", __func__);
		close(fd);
		return -1;
	}
	g_message("file length %d", buf.st_size);
	textbuf = (gchar *)g_malloc0(buf.st_size);
	if (!textbuf) {
		g_message("%s -- Allocate error", __func__);
		return -1;
	}
	g_message("file length %d", buf.st_size);
	if (buf.st_size != read(fd, textbuf, buf.st_size)) {
		g_message("%s -- read file error!", __func__);
		return -1;
	}
	gchar **v = g_strsplit(textbuf, "\n", 1024);
	g_message("%s -- the arrya length is %d", __func__, g_strv_length(v));
	gint file_row = g_strv_length(v);
	int i = 0;
	struct all_dentry config_all;
	memset(&config_all, 0, sizeof(config_all));
	g_strlcpy(config_all.note, v[0], NUM);
	g_message("config_all note is %s", config_all.note);
	//NOTE: we have a '\n' at the last line
	for (i = 0; i < file_row - 1; i++) {
		if (NULL != v[i]) {
			//g_message("%s -- v[%d] is %s", __func__, i, v[i]);
			//g_message("%s -- %c", __func__, v[i][0]);
			if (g_str_has_prefix(v[i], TITLE)) {
				gchar **_v = g_strsplit(v[i], "=", 2);
				g_message("%s  %s", _v[0], _v[1]);
				g_strlcpy(config_all.title, _v[1], NUM);
				g_free(_v);
			}
			if (g_str_has_prefix(v[i], PAGE)) {
				gchar **_v = g_strsplit(v[i], "=", 2);
				g_message("%s  %s", _v[0], _v[1]);
				config_all.pages = atoi(&_v[1][0]);
				g_message("pages %d", config_all.pages);
				g_free(_v);
				config_all.page_title = (char **)g_malloc0(sizeof(char *) * (config_all.pages + 1));
				if (NULL == config_all.page_title) {
					g_message("%s -- Allocate Error!", __func__);
					return -1;
				}
				int j = 1;
				for (j = 1; j <= config_all.pages; j++) {
					config_all.page_title[j] = (char *)g_malloc0(sizeof(char) * NUM);
					if (NULL == config_all.page_title[j]) {
						g_message("%s -- Allocate Error!", __func__);
						return -1;
					}
				}
			}
			int tmp = 1;
			for (tmp = 1; tmp <= config_all.pages; tmp++) {
				gchar title_text[NUM];
				memset(title_text, 0, NUM);
				snprintf(title_text, NUM, "%s%d%s%s", "page", tmp, ".", "title");
				if (g_str_has_prefix(v[i], title_text)) {
					gchar **_v = g_strsplit(v[i], "=", 2);
					g_strlcpy(config_all.page_title[tmp], _v[1], NUM);
					g_strfreev(_v);
				}
			}
		}
	}
	int counter = 0;
	int page_num = 0;
	for (i = 0; i < file_row - 1; i++) {
		gchar name_text[NUM];
		memset(name_text, 0, NUM);
		for (page_num = 1; page_num <= config_all.pages; page_num++) {
			int tmp = 1;
			while (1) {
				snprintf(name_text, NUM, "%s%d%s%d%s%s", "page", page_num, ".", tmp, ".", "name");
				//g_message("name_text is %s", name_text);
				if (g_str_has_prefix(v[i], name_text)) {
					counter += 1;
					break;
				}
				if (tmp >= file_row - 1) {
					break;
				}
				tmp += 1;
			}
		}
	}
	g_message("counter is %d", counter);
	struct dentry item[counter];
	memset(&item, 0, counter);
	int _counter = 0;
	for (i = 0; i < file_row - 1; i++) {
		gchar name_text[NUM];
		memset(name_text, 0, NUM);
		for (page_num = 1; page_num <= config_all.pages; page_num++) {
			int tmp = 1;
			while (1) {
				snprintf(name_text, NUM, "%s%d%s%d%s%s", "page", page_num, ".", tmp, ".", "name");
				//g_message("name_text is %s", name_text);
				if (g_str_has_prefix(v[i], name_text)) {
					g_message("%s", v[i]);
					int j = i;
					g_strlcpy(item[_counter].note, v[i - 1], NUM);
					gchar **_v = g_strsplit(v[i], "=", 2);
					g_strlcpy(item[_counter].name, _v[1], NUM);
					g_strfreev(_v);
					j += 1;
					_v = g_strsplit(v[j], "=", 2);
					g_strlcpy(item[_counter].type, _v[1], NUM);
					g_strfreev(_v);
					j += 1;
					_v = g_strsplit(v[j], "=", 2);
					g_strlcpy(item[_counter].defa, _v[1], NUM);
					g_strfreev(_v);
					j += 1;
					_v = g_strsplit(v[j], "=", 2);
					g_strlcpy(item[_counter].unit, _v[1], NUM);
					g_strfreev(_v);
			//		j += 1;
					_counter += 1;
					break;
				}
				if (tmp >= file_row - 1) {
					break;
				}
				tmp += 1;
			}
		}
	}
	int tmp = 1;
	for (tmp = 1; tmp <= config_all.pages; tmp++) {
		g_message("page title: %s", config_all.page_title[tmp]);
	}
	for (tmp = 0; tmp < counter; tmp++) {
		g_message("name is %s, type is %s, default is %s, unit is %s, note is %s", 
				item[tmp].name, item[tmp].type, item[tmp].defa, item[tmp].unit, item[tmp].note);
	}
	g_free(textbuf);
	g_strfreev(v);
	close(fd);
	return 0;
}

#endif
